package com.poisonh.unifiedauth.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.model.BusinessCard;
import com.poisonh.unifiedauth.utils.ConstantUitls;

import java.util.ArrayList;

/**
 * Created by wish30_4 on 2017/3/24.
 */

public class FriendsAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private static final String CALL_PHONE_PERMISSION = "android.permission.CALL_PHONE";
    private ArrayList<BusinessCard> data;

    public FriendsAdapter(ArrayList<BusinessCard> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            layoutInflater = LayoutInflater.from(context);//实例化
            viewHolder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.friendsadaper_layout, null);
            viewHolder.head_card = (ImageView) view.findViewById(R.id.head_card);
            viewHolder.card_name = (TextView) view.findViewById(R.id.card_name);
            viewHolder.card_company_name = (TextView) view.findViewById(R.id.card_company_name);

            viewHolder.phone_c = (ImageView) view.findViewById(R.id.phone_c);
            viewHolder.phone_c.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {
                        int potion = (int) v.getTag();//根据potion这一行的位置
                        BusinessCard businessCard = data.get(potion);//根据potion这一行的位置取出businessCard对象，通过对象来获取电话号码
                        String mobile = businessCard.getMobile();
                       // Intent intent_phone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));//直接拨打电话：
                        Intent intent_phone = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + mobile));//打开拨打电话界面

                        context.startActivity(intent_phone);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(context, "拨号出错，该功能需要拨号权限！", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            view.setTag(viewHolder);//绑定ViewHolder对象
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
//


            viewHolder.card_name.setText(data.get(i).getName());//姓名 昵称


        viewHolder.card_company_name.setText(data.get(i).getCompany());//公司名称
        String headimg = data.get(i).getHeadImg();
        Glide.with(context).load(ConstantUitls.REQUEDT_IMG_URL + headimg).into(viewHolder.head_card);
        viewHolder.phone_c.setTag(i);//设置把这个控件取出来

        //   BusinessCard card= (BusinessCard) getItem(i);

        //viewHolder.card_name.setText(carList.get(i));

        // viewHolder.friend_cellphone.setText(card.getAddress());
        return view;
    }

    public class ViewHolder {
        ImageView head_card;//头像
        TextView card_name;//姓名
        TextView card_company_name;//公司名称
        ImageView phone_c;//电话图片
    }

}
